import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileChunker
{
	public int peerID;
	
	public FileChunker(int peerID)
	{
		this.peerID = peerID;
	}
	
	public void chunkFile(String fileName, int fileSize, int pieceSize)
	{
		try 
		{
			FileInputStream fis = new FileInputStream(fileName);
		
			// integer division way of accomplishing the ceiling function 
			int numPieces = (fileSize + pieceSize - 1) / pieceSize;
			System.out.println("Number of file pieces: " + numPieces);
			
		    byte buffer[] = new byte[pieceSize];

		    int extLoc = fileName.indexOf(".");
            String fileNameWithoutExt = fileName.substring(0, extLoc);		 
		    String fileExt = fileName.substring(extLoc,fileName.length());
		    
		    // create peer subdirectory if it does not already exist
		    File directory = new File("peer_" + peerID);
		    boolean newDir = directory.mkdirs();
		    
		    System.out.println("Directory was created: " + newDir);
      
		    // read the file into a buffer and write each buffer to a new file
		    int count = 0;
		    while (true)
		    {
		    	int size = fis.read(buffer, 0, pieceSize);
		    	if (size == -1)
		    		break;

		    	String newFile = "peer_" + peerID + "/" + fileName + count;
		    	
		    	System.out.println("File part: " + newFile);
		    	
		    	FileOutputStream fos = new FileOutputStream(newFile);
		    	fos.write(buffer, 0, size);
		    	
		    	count++;
		    	
		    	// flush and close the file output stream
		    	fos.flush();
		    	fos.close();
		    }
		    
		    // close the file input stream
		    fis.close();
		}
		catch (FileNotFoundException e) 
		{
			e.printStackTrace();
		} 
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}
	
	public void recreateFile(String fileName, int fileSize, int pieceSize)
	{
		try 
		{		
			// integer division way of accomplishing the ceiling function 
			int numPieces = (fileSize + pieceSize - 1) / pieceSize;
			System.out.println("Number of file pieces: " + numPieces);
			
			// create file buffer which will hold all of the split files
			// the file parts will be combined to recreate the original file
		    byte buffer[] = new byte[pieceSize];

		    int extLoc = fileName.indexOf(".");
            String fileNameWithoutExt = fileName.substring(0, extLoc);		 
		    String fileExt = fileName.substring(extLoc,fileName.length());
		    
		    // create peer subdirectory if it does not already exist
		    File directory = new File("peer_" + peerID);
		    boolean newDir = directory.mkdirs();
		    
		    System.out.println("Directory was created: " + newDir);
		    
		    // store the file in this peer's subdirectory
		    FileOutputStream fos = new FileOutputStream("peer_" + peerID + "/" + fileName);

		    // loop through all of the file parts, read them and write them to recreate the original file
		    for(int i = 0; i < numPieces; i++)
		    {
		    	//System.out.println(fileName+i);
		    	
		    	FileInputStream fis = new FileInputStream("peer_" + peerID + "/" + fileName+i);

		    	// read in each file part into the buffer and write it
		    	int size = fis.read(buffer, 0, pieceSize);
		    	fos.write(buffer, 0, size);
		    	
		    	// close the file input stream
		    	fis.close();
		    }
		    
		    // flush and close the file output stream
	    	fos.flush();
	    	fos.close();
		}
		catch (FileNotFoundException e) 
		{
			e.printStackTrace();
		} 
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}
}
